package flyln.uestc.edu.a2048;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements GameView.GameListener{

    private GameView gameView;
    private TextView tvScore;
    private TextView tvBest;
    private int score = 0;
    private int bestScore = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        gameView = (GameView)findViewById(R.id.gameview);
        tvScore = (TextView)findViewById(R.id.score);
        tvBest = (TextView)findViewById(R.id.best);

        gameView.setListener(this);
    }

    @Override
    public void addScore(int newScore) {
        score += newScore;
        tvScore.setText(score+"");

        updateBest(score);
    }

    @Override
    public void updateBest(int newScore) {
        SharedPreferences sp = getApplicationContext().getSharedPreferences("game2048", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        bestScore = sp.getInt("best",0);
        if (score > bestScore){
            editor.putInt("best",score);
            bestScore = score;
            tvBest.setText(bestScore+"");
            editor.commit();
        }
    }
}
